package com.ruoyi.deal.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.deal.bo.HyDealBrandQueryBo;
import com.ruoyi.deal.domain.HyDealBrand;
import com.ruoyi.deal.domain.HyDealCategoryBrand;
import com.ruoyi.deal.mapper.HyDealBrandMapper;
import com.ruoyi.deal.mapper.HyDealCategoryBrandMapper;
import com.ruoyi.deal.service.IHyDealBrandService;
import com.ruoyi.deal.service.IHyDealCategoryBrandService;
import com.ruoyi.deal.vo.HyDealBrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 品牌管理Service业务层处理
 *
 * @author lixin
 * @date 2021-05-13
 */
@Service
public class HyDealBrandServiceImpl extends ServicePlusImpl<HyDealBrandMapper, HyDealBrand> implements IHyDealBrandService {

    @Autowired
    private IHyDealCategoryBrandService categoryBrandService;

    @Override
    public HyDealBrandVo queryById(Integer brandId){
        HyDealBrand db = this.baseMapper.selectById(brandId);
        return BeanUtil.toBean(db, HyDealBrandVo.class);
    }

    @Override
    public TableDataInfo<HyDealBrandVo> queryPageList(HyDealBrandQueryBo bo) {

		PagePlus<HyDealBrand, HyDealBrandVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), HyDealBrandVo.class);
		return PageUtils.buildDataInfo(result);
    }

	@Override
	public List<HyDealBrandVo> queryList(HyDealBrandQueryBo bo) {
		return listVo(buildQueryWrapper(bo), HyDealBrandVo.class);
	}


	private LambdaQueryWrapper<HyDealBrand> buildQueryWrapper(HyDealBrandQueryBo bo) {
		LambdaQueryWrapper<HyDealBrand> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getBrandName()), HyDealBrand::getBrandName, bo.getBrandName());
		lqw.eq(StrUtil.isNotBlank(bo.getBrandInitial()), HyDealBrand::getBrandInitial, bo.getBrandInitial());
		lqw.eq(bo.getStatus() != null, HyDealBrand::getStatus, bo.getStatus());
		lqw.orderByAsc(HyDealBrand::getOrderNum);
		return lqw;
	}

    @Override
    public int insertByAddBo(HyDealBrand brand) {

        // 新增前校验品牌信息
        validEntityBeforeSave(brand);

        // 新增品牌信息
        baseMapper.insert(brand);

        // 新增分类品牌关联表
        return insertCategoryBrand(brand);

    }

    @Override
    public int updateBrand(HyDealBrand brand) {

        // 修改前数据校验
        validEntityBeforeSave(brand);

        // 品牌信息修改
        this.updateById(brand);

        // 删除分类与品牌关联
        categoryBrandService.remove(new LambdaQueryWrapper<HyDealCategoryBrand>().eq(HyDealCategoryBrand::getBrandId, brand.getBrandId()));

        // 新增分类品牌关联表
        return insertCategoryBrand(brand);
    }

    /**
     * 新增分类品牌信息
     *
     * @param brand 品牌对象
     */
    public int insertCategoryBrand(HyDealBrand brand) {
        int rows = 1;
        // 新增用户与角色管理
        List<HyDealCategoryBrand> list = new ArrayList<HyDealCategoryBrand>();
        for (Integer categoryId : brand.getCategoryIds()) {
            HyDealCategoryBrand categoryBrand = new HyDealCategoryBrand();
            categoryBrand.setBrandId(brand.getBrandId());
            categoryBrand.setCategoryId(categoryId);
            list.add(categoryBrand);
        }
        if (list.size() > 0) {
            categoryBrandService.saveBatch(list);
        }
        return rows;
    }

    @Override
    public Boolean deleteWithValidByIds(List<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }

    /**
     * 校验品牌名称是否唯一
     *
     * @param brand 品牌信息
     * @return 结果
     */
    @Override
    public String checkBrandNameUnique(HyDealBrand brand) {

        Integer brandId = Validator.isNull(brand.getBrandId()) ? -1 : brand.getBrandId();
        HyDealBrand info = getOne(new LambdaQueryWrapper<HyDealBrand>()
                .eq(HyDealBrand::getBrandName, brand.getBrandName()).last("limit 1"));
        if (Validator.isNotNull(info) && info.getBrandId().longValue() != brandId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }



    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(HyDealBrand entity){
        //TODO 做一些数据校验,如唯一约束
    }
}
